{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ddacac8c-40ed-456a-a71c-8e278517865c",
   "metadata": {},
   "source": [
    "# 有关字典的常见操作\n",
    "`dict` 是 Python 的哈希表实现，通过键值对存储数据，查找、插入和删除操作平均时间复杂度为 O(1)。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2bf234a0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 初始化字典\n",
    "hashmap = {1: \"one\", 2: \"two\", 3: \"three\"}\n",
    "\n",
    "# 是否为空，输出：False\n",
    "print(len(hashmap) == 0)\n",
    "\n",
    "# 大小，输出：3\n",
    "print(len(hashmap))\n",
    "\n",
    "# 查找键\n",
    "# 输出：Key 2 -> two\n",
    "if 2 in hashmap:\n",
    "    print(f\"Key 2 -> {hashmap[2]}\")\n",
    "else:\n",
    "    print(\"Key 2 not found.\")\n",
    "\n",
    "# 获取键对应的值，不存在则返回 None\n",
    "# 输出：None\n",
    "print(hashmap.get(4))\n",
    "\n",
    "# 插入新键值对\n",
    "hashmap[4] = \"four\"\n",
    "\n",
    "# 获取新插入的值，输出：four\n",
    "print(hashmap[4])\n",
    "\n",
    "# 删除键 3\n",
    "del hashmap[3]\n",
    "\n",
    "# 检查删除后\n",
    "if 3 in hashmap:\n",
    "    print(f\"Key 3 -> {hashmap[3]}\")\n",
    "else:\n",
    "    print(\"Key 3 not found.\")\n",
    "# 输出：Key 3 not found.\n",
    "\n",
    "# 遍历字典\n",
    "# 输出：\n",
    "# 1 -> one\n",
    "# 2 -> two\n",
    "# 4 -> four\n",
    "for k, v in hashmap.items():\n",
    "    print(f\"{k} -> {v}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2ce122c3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': [1, 3], 'b': [2]}\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "from collections import defaultdict\n",
    "# 创建了一个 `defaultdict` 对象，并且指定了 `list` 作为默认工厂函数。\n",
    "# 访问 `lookup` 中不存在的键时，`defaultdict` 会自动为那个键创建一个空列表作为其值。\n",
    "# 这样，你就不需要事先检查键是否存在于字典中，也不需要在访问之前手动为键设置默认值。\n",
    "lookup = defaultdict(list)\n",
    "# 添加一些键值对\n",
    "lookup['a'].append(1)\n",
    "lookup['b'].append(2)\n",
    "lookup['a'].append(3)\n",
    "\n",
    "# 打印结果\n",
    "print(dict(lookup))  # 输出：{'a': [1, 3], 'b': [2]}\n",
    "\n",
    "# 访问不存在的键\n",
    "print(lookup['c'])  # 输出：[]，因为'c'不存在，所以返回默认值list()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c719374",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b54b93e8-ec50-4998-89ad-c939c8bc4b24",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用大括号创建字典\n",
    "person = {'name': 'James', 'age': 88, 'gender': 'male'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b213643c-48df-4d03-a0c2-5e4cf3c2b858",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用 dict() 函数创建字典\n",
    "fruits = dict(apple=88, banana=888, cherry=8888)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d25c260d-8cca-428a-83ca-b0320c476ee2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "James\n",
      "8888\n"
     ]
    }
   ],
   "source": [
    "# 访问字典中的值\n",
    "print(person['name']) \n",
    "print(fruits['cherry'])  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "aeac445b-6747-450c-bbd4-a73bef446dc1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'James', 'age': 28, 'gender': 'male'}\n"
     ]
    }
   ],
   "source": [
    "# 修改字典中的值\n",
    "person['age'] = 28\n",
    "print(person)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8a020b32-64e4-49d5-a8f1-7ed9436110a3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'James', 'age': 28, 'gender': 'male', 'city': 'Toronto'}\n"
     ]
    }
   ],
   "source": [
    "# 添加键值对\n",
    "person['city'] = 'Toronto'\n",
    "print(person) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c7774642-5da4-4bf1-b106-1d46a445ff61",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'James', 'age': 28, 'city': 'Toronto'}\n"
     ]
    }
   ],
   "source": [
    "# 删除键值对\n",
    "del person['gender']\n",
    "print(person)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d8a8d3b7-50d6-4eb0-805c-6bd613a6c537",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['name', 'age', 'city'])\n",
      "dict_values(['James', 28, 'Toronto'])\n",
      "dict_items([('name', 'James'), ('age', 28), ('city', 'Toronto')])\n"
     ]
    }
   ],
   "source": [
    "# 获取键、值、键值对列表\n",
    "print(person.keys())  \n",
    "print(person.values())  \n",
    "print(person.items())  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82771c2e",
   "metadata": {},
   "source": [
    "# 多层字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4f3e4ce5",
   "metadata": {},
   "outputs": [],
   "source": [
    "two_layer_dict = {\n",
    "    \"data\":{\n",
    "        \"num\":1\n",
    "    }\n",
    "}\n",
    "# 访问\n",
    "x = two_layer_dict[\"data\"][\"num\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f9bf1e08",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3864199c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import sys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63f3e2b3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 请在此输入您的代码\n",
    "original_data = input()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "f1a20d5c",
   "metadata": {},
   "outputs": [],
   "source": [
    "original_data = \"Asdzxc56\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "0706417b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def solution(original_data):\n",
    "  input_data = original_data.lower()\n",
    "  if len(input_data) == 0:\n",
    "    return False\n",
    "  my_dict = dict()\n",
    "  for item in input_data:\n",
    "    if item in my_dict:\n",
    "      return False\n",
    "    my_dict[item] = 1\n",
    "  return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "41e6ac2c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "YES\n"
     ]
    }
   ],
   "source": [
    "if solution(original_data) == True:\n",
    "  print(\"YES\")\n",
    "else:\n",
    "  print(\"NO\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "science39",
   "language": "python",
   "name": "science39"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
